package com.atguigu.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gulimall.common.utils.PageUtils;
import com.atguigu.gulimall.common.utils.Query;

import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        LambdaQueryWrapper<SkuInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        String brandId = (String) params.get("brandId");
        String cateLogId = (String) params.get("cateLogId");
        String min = (String) params.get("min");
        String max = (String) params.get("max");

        if (StringUtils.isNotEmpty(key)){
            queryWrapper.and(obj -> {
                obj.eq(SkuInfoEntity::getSkuName,key).or()
                        .like(SkuInfoEntity::getSkuId,key);
            });
        }
        queryWrapper.eq(StringUtils.isNotEmpty(brandId) && !brandId.equals("0")
                ,SkuInfoEntity::getBrandId,brandId);
        queryWrapper.eq(StringUtils.isNotEmpty(cateLogId) && !cateLogId.equals("0")
                ,SkuInfoEntity::getCatalogId,cateLogId);
        if((StringUtils.isNotEmpty(min) &&!min.equals("0")) || (StringUtils.isNotEmpty(max) && !max.equals("0"))) {
            queryWrapper.ge(StringUtils.isNotEmpty(min), SkuInfoEntity::getPrice, min);
            queryWrapper.le(StringUtils.isNotEmpty(max), SkuInfoEntity::getPrice, max);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}